$(function () {
    findByItem(1);
    $.post("/user/getUser",null,function (info) {
        var obj = JSON.parse(info);
        if(obj == null){
            $("#loginInfoDiv").html("你好,请先<a href='/html/login.html' class='lauiy-btn layui-btn-warm'>登陆</a>")
        }else {
            $("#loginInfoDiv").html("您好,"+obj.name);
        }
    })

})

function findByItem(pageNO) {
    $.post("/product/findByItem",$("#searchForm").serialize()+"&pageNO="+pageNO,function (pageObj) {
        var str = "";
        for(var i=0;i<pageObj.list.length;i++){
            var obj = pageObj.list[i];
            str += `<div class='layui-col-md4' style="text-align: center">
                        <img src="/html/productImg/${obj.imgPath}" width="200" height="260"><br>
                        ${obj.name}<br>
                        单价：${obj.price}<br>
                        已售出：${obj.saleNum}<br>
                        <a class="layui-btn layui-btn-primary" 
                         href="/html/customer/shopCar.html?id=${obj.id}">购买</a>
                    </div>`;
        }
        $("#productDiv").html(str);

        if(pageNO == 1){
            layui.use('laypage', function(){
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'cutPageDiv', //注意，这里的 test1 是 ID，不用加 # 号
                    count: pageObj.total, //数据总数，从服务端得到
                    limit:pageObj.pageSize,
                    jump:function (obj,first) {
                        if(!first){
                            findByItem(obj.curr)
                        }
                    }
                })
            })
        };
    })
}

function showShopCar() {
    $.post("/user/getUser",null,function (info) {
        var obj = JSON.parse(info);
        if(obj != null){
            location.href="/html/customer/shopCar.html";
        }else {
            location.href="/html/login.html";
        }
    })
}

function myOrder() {
    $.post("/user/getUser",null,function (info) {
        var obj = JSON.parse(info);
        if(obj != null){
            location.href="/html/customer/myOrder.html?userId="+obj.id;
        }else {
            location.href="/html/login.html"
        }

    })
}